package com.kevin.demo;

import org.apache.log4j.Logger;
import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzDemo {
	public static Scheduler scheduler;
	private static Logger logger = Logger.getLogger(QuartzDemo.class);

	QuartzDemo() {
		initQuartz();
	}

	public static void main(String[] args) {
		try {
			long stime = System.currentTimeMillis();
			new QuartzDemo();
//			scheduler.standby();
//			Thread.sleep(10000);

//			scheduler.start();
			long etime = System.currentTimeMillis();
			logger.info((etime-stime)/1000);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}

	}

	private static void initQuartz() {
		try {
			scheduler = StdSchedulerFactory.getDefaultScheduler();
			JobDetail jobDetail = JobBuilder.newJob(JobDemo.class).withIdentity("job").build();
			jobDetail.getJobDataMap().put("name", "testJob");
			jobDetail.getJobDataMap().put("org.quartz.threadPool.threadCount", "1");
			jobDetail.getJobDataMap().put("org.quartz.jobStore.misfireThreshold", "1");
			CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0/1 31 * * * ?");
			// ——不触发立即执行
			// ——等待下次Cron触发频率到达时刻开始按照Cron频率依次执行
			cronScheduleBuilder = cronScheduleBuilder.withMisfireHandlingInstructionDoNothing();// 错过多少个trigger就补回多少个

			// ——以错过的第一个频率时间立刻开始执行
			// ——重做错过的所有频率周期后
			// ——当下一次触发频率发生时间大于当前时间后，再按照正常的Cron频率依次执行
			// cronScheduleBuilder =
			cronScheduleBuilder.withMisfireHandlingInstructionFireAndProceed();// 错过n个trigger就补回N+1个
			// cronScheduleBuilder =
			cronScheduleBuilder.withMisfireHandlingInstructionIgnoreMisfires();// 错过多少个trigger就补回多少个
			// cronScheduleBuilder.
			CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("job").withSchedule(cronScheduleBuilder)
					.build();
			scheduler.scheduleJob(jobDetail, trigger);
			scheduler.start();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}
}